/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2021/10/26
 * Time: 9:26
 * Description:
 */
class Node {
    public int val;
    public Node prev;
    public Node next;

    public Node(int val) {
        this.val = val;
    }
}
public class MyDoubleList {
    public Node head;
    //public Node head = new Node(-1);
    public Node last;


    //得到长度
    public int size() {
        int count = 0;
        Node cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void display(){
        Node cur = this.head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //查找是否包含关键字key是否在
    public boolean contains(int key){
        Node cur = this.head;
        while (cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data){
        Node node = new Node(data);
        if(this.head == null) {
            this.head = node;
            this.last = node;
        }else {
            node.next = head;
            this.head.prev = node;
            this.head = node;
        }
    }
    //尾插法
    public void addLast(int data){
        Node node = new Node(data);
        if(this.head == null) {
            this.head = node;
            this.last = node;
        }else {
            this.last.next = node;
            node.prev = this.last;
            this.last = node;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        //1、index 是否合法
        if(index < 0 || index > size()) {
            System.out.println("index不合法！");
            return;
        }
        if(index == 0 ) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        Node cur = findIndexNode(index);
        Node node = new Node(data);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }

    private Node findIndexNode(int index) {
        Node cur = this.head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        Node cur = this.head;
        while (cur != null) {
            if(cur.val == key) {
                if(cur == this.head) {//删除的是头节点
                    this.head = this.head.next;
                    if(this.head != null) {//如果只有一个头节点
                        this.head.prev = null;
                    }else {
                        this.last = null;//一定不能忘记  否则会发生泄漏
                    }
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next != null) {//中间的节点
                        cur.next.prev = cur.prev;
                    }else {//尾巴
                        last = cur.prev;
                    }
                }
                return;
            }else {
                cur = cur.next;
            }
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        Node cur = this.head;
        while (cur != null) {
            if(cur.val == key) {
                if(cur == this.head) {//删除的是头节点
                    this.head = this.head.next;
                    if(this.head != null) {//如果只有一个头节点
                        this.head.prev = null;
                    }else {
                        this.last = null;//一定不能忘记  否则会发生泄漏
                    }
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next != null) {//中间的节点
                        cur.next.prev = cur.prev;
                    }else {//尾巴
                        last = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }

    public void clear() {
        while (this.head != null) {
            Node cur = this.head.next;
            this.head.next = null;
            this.head.prev = null;
            this.head = cur;
        }
        this.last = null;
    }//jps    jmap  -histo:live  进程号  > e:\\log.txt

}
